<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/6/11
 * Time: 10:48
 */

namespace App\Services;


use App\Model\FriendModel;
use App\Model\UserModel;


class FriendService
{
    /**
     * 获取联合id
     */
    public function getUnionInfo($userId,$friend_user_id){
        try {

            $friendInfo = FriendModel::create()->get(['user_id' => $userId, 'friend_user_id' => $friend_user_id]);
            if ($friendInfo) {
                return $friendInfo;
            }
           #创建关系
            $unionid = HelpService::createUnionId($userId);
            $friendData = ['user_id'=>$userId,
                'friend_user_id'=>$friend_user_id,
                'unionid'=>$unionid,
                'status'=>FriendModel::FRIEND_STATUS_TEMP,
                'created_at'=>HelpService::msectimeToStr(HelpService::msectime()),
                'updated_at'=>HelpService::msectimeToStr(HelpService::msectime())
            ];
            FriendModel::create()->data($friendData)->save();

            $beFriendData = [
                'user_id'=>$friend_user_id,
                'friend_user_id'=>$userId,
                'unionid'=>$unionid,
                'status'=>FriendModel::FRIEND_STATUS_TEMP,
                'created_at'=>HelpService::msectimeToStr(HelpService::msectime()),
                'updated_at'=>HelpService::msectimeToStr(HelpService::msectime())
            ];
            FriendModel::create()->data($beFriendData)->save();
          return    FriendModel::create()->get(['user_id' => $userId, 'friend_user_id' => $friend_user_id]);
        }catch(\Throwable $t){
            return ReponseCodeService::CODE_SERVICE_UNAVAILABLE;
        }
    }

    /**
     * 是否是好友
     */
    function isFriend($userId,$friend_user_id)
    {
        try {
            $fiendModel = new FriendModel();
            $friendInfo = $fiendModel->get(['user_id' => $userId, 'friend_user_id' => $friend_user_id])->field('id');
            if ($friendInfo) {
                return $friendInfo;
            }
            return false;
        } catch (\Throwable $t) {
                return ReponseCodeService::CODE_SERVICE_UNAVAILABLE;
        }
    }

}